﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Xml.Serialization;

namespace Urbania.GUI.Elements
{
    [Serializable]
    [XmlRoot("Frame")]
    [XmlInclude(typeof(GUIElement))]
    public class Frame : GUIElement
    {
        private List<GUIElement> elements;

        public List<GUIElement> Elements { get { return this.elements; } set { this.elements = value; } }

        public Frame()
        {
            this.lua = null;
            elements = new List<GUIElement>();
        }

        public Frame(ref LuaVM lua)
        {
            elements = new List<GUIElement>();
            this.lua = lua;
        }

        public override void Update(GameTime gameTime)
        {
            foreach (GUIElement e in elements)
                e.Update(gameTime);
        }

        public override void Update(GameTime gameTime, MouseState mouse)
        {
            foreach (GUIElement e in elements)
                e.Update(gameTime, mouse);  
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (GUIElement e in elements)
            {
                e.Draw(gameTime, spriteBatch);
            }
        }

        public void AddPopUp(PopUp p)
        {
            if (p.Parent == this)
                elements.Add(p);
        }

        public void AddPopUp(Rectangle bounds)
        {
            elements.Add(new PopUp(bounds, ref lua, this));
        }

        public void AddPopUp(Rectangle bounds, Color bg)
        {
            elements.Add(new PopUp(bounds, bg, ref lua, this));
        }

        public void AddButton(Button b)
        {
            if(b.Parent == this)
                elements.Add(b);
        }

        public void AddButton(Vector2 position, int designType, string buttonName, string buttonText, string buttonScript)
        {
            elements.Add(new Button(buttonName, position, buttonText, designType, ref lua, buttonScript, this));
        }

        public void SetParents(ref LuaVM lua)
        {
            foreach (GUIElement e in elements)
            {
                e.SetParent(this);
                e.SetLua(ref lua);
                e.Load();
                if (e is PopUp)
                {
                    PopUp p = (PopUp)e;
                    p.SetParents(ref lua);
                }
            }
        }
    }
}
